package authority

import (
	"ginfwk/adapter/base/controller"
	dto "ginfwk/application/dto/admin"
	"ginfwk/common/mock/admin"
	"ginfwk/common/mock/mhelper"
	"ginfwk/infrastructure/helper/ghttp"
	"github.com/gin-gonic/gin"
	"net/http"
)

type Authority struct {
	controller.BaseController
}

func (c Authority) List(g *gin.Context) interface{} {
	c.Init(g)

	type Param struct {
		PageIndex int    `json:"pageIndex" form:"pageIndex"`
		PageSize  int    `json:"pageSize" form:"pageSize"`
		AuthName  string `json:"authName" form:"authName"`
		AuthTag   string `json:"authTag" form:"authTag"`
	}
	var param Param
	if err := g.ShouldBindQuery(&param); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	itemList := admin.GetAuthList()
	pageList := mhelper.Pagination(itemList, param.PageIndex, param.PageSize)

	return ghttp.Success(gin.H{
		"total": len(itemList),
		"list":  pageList,
	})
}

func (c Authority) Info(g *gin.Context) interface{} {
	c.Init(g)

	type Param struct {
		Id string `json:"id" form:"id"`
	}
	var param Param
	if err := g.ShouldBindQuery(&param); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	var info *dto.AuthInfo
	itemList := admin.GetAuthList()
	for _, item := range itemList {
		if item.Id == param.Id {
			info = &item
			break
		}
	}
	return ghttp.Success(info)
}

func (c Authority) Delete(g *gin.Context) interface{} {
	c.Init(g)

	type Param struct {
		Ids []string `json:"ids" form:"ids"`
	}
	var param Param
	if err := g.ShouldBindJSON(&param); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	return ghttp.Fail(http.StatusBadRequest, "删除失败,该用户已不存在!")
	//return ghttp.Success(param)
}

func (c Authority) Save(g *gin.Context) interface{} {
	c.Init(g)

	type Param struct {
		Id          string `json:"id" form:"id"`
		AuthName    string `json:"authName" form:"authName"`
		AuthTag     string `json:"authTag" form:"authTag"`
		CategoryTag string `json:"categoryTag" form:"categoryTag"`
		Status      int    `json:"status" form:"status"`
	}
	var param Param
	if err := g.ShouldBindJSON(&param); err != nil {
		return ghttp.Fail(-1, "信息错误:"+err.Error())
	}

	param.Id = "xxxx3"
	return ghttp.Success(param)
}
